######################################################/
## Anticipated Election Result and Protest Voting    ##
## -- Replication Material (Analyses)                ##
## by: Christian Schimpf                             ##
## Version: July 01, 2019                            ##
## R-Version: 3.4.0.                                 ##
######################################################/

#############################################################################/
#### Part 3 - Analayses (Descriptives, Main Analyses, Robustness Checks) #### 
############################################################################/

# Note: a helpful source to generate the figures was the following:
# http://bradleyboehmke.github.io/tutorials/barchart (Last accessed: July 1, 2019)

# Before getting into the analyses, I subset the dataset as described in the main text
# (see section "Research Strategy"; abstainers are also excluded - see paper for more information):

df_PrevVote <- dfCan2015[which(dfCan2015$SD9==dfCan2015$PartyPref_FoUp &
                                 dfCan2015$NonSince_FoUp!="2"),]

#############################################################################/
#### /// 3A: Descriptive Statistics                                       ####
#############################################################################/

#### >>> Table A10.1 (Supplementary Material) ####
# Descriptive Stats of all relevant variables:

#create a subset with all variables included in model
selection_desc <- c("NonSince_FoUp_Dum", "IdeoDis_FoUp", "Comp_FoUp_Dummy", "ExpCom_N",
                    "WinChaPref_Relative", "OutcomeImportance", "OutcomeImportance_National",
                    "ID_HighestRatedParty_FoUp", "LocalLeader_Congruence_FoUp",
                    "LeaderPartyPref_CongruenceCat_FoUp", "Gender", "Age", "Edu",
                    "Province")

df_selection_desc <- df_PrevVote[selection_desc]

#generate table with descriptives (cf. Table A10.1 in the Supplementary Material of the Paper)
stargazer(df_selection_desc, digits = 2, type = "text")
# Note: stargazer only includes numeric variables. Factor variables were tabbed separately. 

#### >>> Figure 1 ####

## Plot: Ideological Distance by Dependent Variable (DV - ties broken up using follow up question):


#Add labels to dependent variable:
levels(df_PrevVote$NonSince_FoUp_Dum) <- c("Sincere", "Insincere")

# Use tidy to find percentages of sincere/insincere voting by ideological congruence level:
proportion1A <- df_PrevVote[!is.na(df_PrevVote$NonSince_FoUp_Dum) & 
                            !is.na(df_PrevVote$IdeoDis_FoUp) & 
                            df_PrevVote$IdeoDis_FoUp<6,] %>%
  group_by(IdeoDis_FoUp, NonSince_FoUp_Dum) %>%
  tally() %>%
  mutate(pct = n / sum(n)) 

# Add column with labels for plot
proportion1A <- proportion1A %>%
  group_by(NonSince_FoUp_Dum) %>%
  mutate(label_y = pct)

# Generate plot 1A:
p1A <- ggplot(proportion1A, aes(factor(IdeoDis_FoUp), pct, fill = factor(NonSince_FoUp_Dum))) +
  geom_bar(stat = "identity", color = "grey40") +
  geom_text(aes(label = round(pct, 2), y = label_y), vjust = 1.4, color = "black") +
  scale_fill_manual(values = c("#CCCCCC", "#666666")) +
  xlab("Ideological incongruence")+
  ylab("Percentage")+
  theme(legend.position="bottom", legend.title=element_text(size=10.5), 
        legend.text=element_text(size=9))+ 
  labs(fill="Vote")+ 
  ggtitle("1.1 - Ideological incongruence")

## Plot: Competence to handle MIP by Dependent Variable (DV - ties broken up using follow up question):

# Shorten labels for values for competence variable by generating new variable with 
# identical values but shorter levels:
df_PrevVote$Comp_FoUp_Dummy_Plot <- df_PrevVote$Comp_FoUp_Dummy
levels(df_PrevVote$Comp_FoUp_Dummy_Plot) <- c("Most competent", "Not most competent")

# Add lines breaks to levels:
#https://www.r-bloggers.com/line-breaks-between-words-in-axis-labels-in-ggplot-in-r/ (last accessed
# Sept. 25, 2018)
#levels(df_PrevVote$Comp_FoUp_Dummy_Plot) <- gsub(" ", "\n", levels(df_PrevVote$Comp_FoUp_Dummy_Plot))

# Use tidy to find percentages of sincere/insincere voting by ideological congruence level:
proportion1B <- df_PrevVote[!is.na(df_PrevVote$NonSince_FoUp_Dum) & 
                            !is.na(df_PrevVote$Comp_FoUp_Dummy_Plot),] %>%
  group_by(Comp_FoUp_Dummy_Plot, NonSince_FoUp_Dum) %>%
  tally() %>%
  mutate(pct = n / sum(n))

# Add column with labels for plot
proportion1B <- proportion1B %>%
  group_by(NonSince_FoUp_Dum) %>%
  mutate(label_y = pct)

# Generate plot 1A:
p1B <- ggplot(proportion1B, aes(factor(Comp_FoUp_Dummy_Plot), pct, fill = factor(NonSince_FoUp_Dum))) +
  geom_bar(stat = "identity", color = "grey40") +
  geom_text(aes(label = round(pct, 2), y = label_y), vjust = 1.35, color = "black") +
  scale_fill_manual(values = c("#CCCCCC", "#666666")) +
  xlab("Party competence")+
  ylab("Percentage")+
  theme(legend.position="bottom", legend.title=element_text(size=10.5), 
        legend.text=element_text(size=9))+ 
  labs(fill="Vote")+ 
  ggtitle("1.2 - Party incompetence")


## Use grid arrange to combine both plots into one:
png("Figure1_Descriptive_Plots.png",
    units="in", width=6, height=3.5, res=300)
grid.arrange(p1A, p1B, ncol=2)
dev.off()

#Save in Tiff Format
tiff("Figure1_Descriptive_Plots_TIFF.tiff",
     units="in", width=6, height=3.5, res=300, compression = "lzw")
grid.arrange(p1A, p1B, ncol=2)
dev.off()


#############################################################################/
#### /// 3B: Main Analyses - Logit Models                                ####
#############################################################################/

# Before running the analyses, the variable party competence
# is renamned and its levels are renmaned, too, for plotting
# purposes (i.e., the plots of the interaction terms):
names(df_PrevVote)[names(df_PrevVote) == "Comp_FoUp_Dummy"] <- "Competence"
levels(df_PrevVote$Competence) <- c("Most competent", ": Not most competent")

#names(df_PrevVote)[names(df_PrevVote) == "Comp_FoUp_Dummy"] <- "Competence"
#levels(df_PrevVote$Competence) <- c("Most competent", ": Not most competent")
#df_PrevVote$ExpCom_N <- as.numeric(df_PrevVote$ExpCom)
#df_PrevVote$ExpCom_N <- 4-df_PrevVote$ExpCom_N

#### >>> Basic Model (without covariates; not included in the paper ####

Logit_1 <- glm(NonSince_FoUp_Dum ~ IdeoDis_FoUp + Competence,
               family = "binomial",
               data=df_PrevVote)
summary(Logit_1)


#### >>> Basic Model (with covariates; Model 1 in Table 1) ####
# -> The full model is also displayed in Table B1 in the Supplementary Material

Logit_2 <- glm(NonSince_FoUp_Dum ~ 
                 IdeoDis_FoUp + 
                 Competence + 
                 WinChaPref_Relative+ 
                 ExpCom_N + 
                 OutcomeImportance + 
                 OutcomeImportance_National + 
                 ID_HighestRatedParty_FoUp + 
                 LocalLeader_Congruence_FoUp + 
                 LeaderPartyPref_CongruenceCat_FoUp +
                 Gender + Age + Edu + Province,
               family = "binomial",
               data=df_PrevVote)
summary(Logit_2)

### Using Zelig to interpret effects:
# The tables from the ZELIG Models were not included in the text. The ZELIG function of simulating
# quantities of interest, however, was used to asses the results further and some of these findings
# have found their way into the main text (see Results Section in the paper)
#Helpful Source: http://www.ucl.ac.uk/~uctqiax/PUBLG100/2016/week9/seminar9.html

logitmodel2 <- zelig(NonSince_FoUp_Dum ~ 
                       IdeoDis_FoUp + 
                       Competence + 
                       WinChaPref_Relative+ 
                       ExpCom_N + 
                       OutcomeImportance + 
                       OutcomeImportance_National + 
                       ID_HighestRatedParty_FoUp + 
                       LocalLeader_Congruence_FoUp + 
                       LeaderPartyPref_CongruenceCat_FoUp +
                       Gender + Age + Edu + Province,
                     model="logit", data=df_PrevVote)

# A) Interpret effects of ideological distances:
hypindA <- setx(logitmodel2, IdeoDis_FoUp= 0,
                Competence="Most competent",
                WinChaPref_Relative=mean(df_PrevVote$WinChaPref_Relative, na.rm=T),
                ExpCom_N = mean(df_PrevVote$ExpCom_N, na.rm=T),
                OutcomeImportance=mean(df_PrevVote$OutcomeImportance, na.rm=T),
                OutcomeImportance_National=mean(df_PrevVote$OutcomeImportance_National, na.rm=T),
                ID_HighestRatedParty_FoUp=1,
                LocalLeader_Congruence_FoUp=0,
                LeaderPartyPref_CongruenceCat_FoUp=0,
                Gender="Female",
                Age=mean(df_PrevVote$Age, na.rm=T),
                Edu="No University Education",
                Province="Ontario")

hypindB <- setx(logitmodel2, IdeoDis_FoUp= 3,
                Competence="Most competent",
                WinChaPref_Relative=mean(df_PrevVote$WinChaPref_Relative, na.rm=T),
                ExpCom_N = mean(df_PrevVote$ExpCom_N, na.rm=T),
                OutcomeImportance=mean(df_PrevVote$OutcomeImportance, na.rm=T),
                OutcomeImportance_National=mean(df_PrevVote$OutcomeImportance_National, na.rm=T),
                ID_HighestRatedParty_FoUp=1,
                LocalLeader_Congruence_FoUp=0,
                LeaderPartyPref_CongruenceCat_FoUp=0,
                Gender="Female",
                Age=mean(df_PrevVote$Age, na.rm=T),
                Edu="No University Education",
                Province="Ontario")

Hyp1 <- Zelig::sim(logitmodel2, x = hypindA, x1 = hypindB)

Hyp1
#-> used for interpretation of substantial results. 




# B) Interpret effects of competency:

hypindAa <- setx(logitmodel2, IdeoDis_FoUp= mean(df_PrevVote$IdeoDis_FoUp, na.rm=T),
                 Competence="Most competent",
                 WinChaPref_Relative=mean(df_PrevVote$WinChaPref_Relative, na.rm=T),
                 ExpCom_N = mean(df_PrevVote$ExpCom_N, na.rm=T),
                 OutcomeImportance=mean(df_PrevVote$OutcomeImportance, na.rm=T),
                 OutcomeImportance_National=mean(df_PrevVote$OutcomeImportance_National, na.rm=T),
                 ID_HighestRatedParty_FoUp=1,
                 LocalLeader_Congruence_FoUp=0,
                 LeaderPartyPref_CongruenceCat_FoUp=0,
                 Gender="Female",
                 Age=mean(df_PrevVote$Age, na.rm=T),
                 Edu="No University Education",
                 Province="Ontario")

hypindBa <- setx(logitmodel2, IdeoDis_FoUp= mean(df_PrevVote$IdeoDis_FoUp, na.rm=T),
                 Competence="Not most competent",
                 WinChaPref_Relative=mean(df_PrevVote$WinChaPref_Relative, na.rm=T),
                 ExpCom_N = mean(df_PrevVote$ExpCom_N, na.rm=T),
                 OutcomeImportance=mean(df_PrevVote$OutcomeImportance, na.rm=T),
                 OutcomeImportance_National=mean(df_PrevVote$OutcomeImportance_National, na.rm=T),
                 ID_HighestRatedParty_FoUp=1,
                 LocalLeader_Congruence_FoUp=0,
                 LeaderPartyPref_CongruenceCat_FoUp=0,
                 Gender="Female",
                 Age=mean(df_PrevVote$Age, na.rm=T),
                 Edu="No University Education",
                 Province="Ontario")

Hyp1a <- Zelig::sim(logitmodel2, x = hypindAa, x1 = hypindBa)

Hyp1a
#-> used for substantial interpretation of the results. 



#### >>> Basic Model (with covariates and interactions for expected win chances; Model 2A in Table 1) ####
# -> The full model is also displayed in Table B1 in the Supplementary Material (Model 2A)

Logit_2A <- glm(NonSince_FoUp_Dum ~ 
                  IdeoDis_FoUp*WinChaPref_Relative + 
                  Competence + 
                  ExpCom_N + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote)  
summary(Logit_2A)


#### >>> Figure 2.1 - marginal effects plot 1 ####
# Plot is based on Model "Logit_2A" above
intplot2A <- interplot(m = Logit_2A, var1 = "IdeoDis_FoUp", var2 = "WinChaPref_Relative", hist = T,
                       adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived chances of preferred \n party to win constituency")+
  ylab("Marginal effect of ideological \n incongruence on insincere voting")+
  ggtitle("2.1- Ideological incongruence*Win \n chance")+
  scale_x_continuous(labels=c("0.00" = "0. None", "0.25" = "0.25", "0.50" = "0.50",
                              "0.75" = "0.75", "1.00" = "1. High"))

#### >>> Basic Model (with covariates and interactions for competence*win chance; Model 2B in Table 1) ####
# -> The full model is also displayed in Table B1 in the Supplementary Material (Model 2B)

Logit_2B <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp + 
                  Competence*WinChaPref_Relative + 
                  ExpCom_N + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                         family = "binomial",
                         data=df_PrevVote)
summary(Logit_2B)


#### >>> Figure 2.2 - marginal effects plot 2 ####
# Plot is based on Model "Logit_2B" above
intplot2B <- interplot(m = Logit_2B, var1 = "Competence", var2 = "WinChaPref_Relative", hist = T,
                       adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived chances of preferred \n party to win constituency")+
  ylab("Marginal effect of party \n incompetence on insincere voting")+
  ggtitle("2.2- Party incompetence*Win chance")+
  scale_x_continuous(labels=c("0.00" = "0. None", "0.25" = "0.25", "0.50" = "0.50",
                              "0.75" = "0.75", "1.00" = "1. High"))

#### >>> Basic Model (with covariates and interactions for expected electoral competition; Model 3A in Table 1) ####
# -> The full model is also displayed in Table B1 in the Supplementary Material (Model 3A)

Logit_2C <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp*ExpCom_N+ 
                  Competence + 
                  WinChaPref_Relative + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=df_PrevVote)  
summary(Logit_2C)


#### >>> Figure 3.1 - marginal effects plot 3 ####
# Plot is based on Model "Logit_3A" above
intplot2C <- interplot(m = Logit_2C, var1 = "IdeoDis_FoUp", var2 = "ExpCom_N", hist = T,
                       adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived, expected electoral \n competition in constituency")+
  ylab("Marginal effect of ideological \n incongruence on protest voting")+
  ggtitle("3.1- Ideological incongruence* \n Electoral competition")+
  scale_x_continuous(labels=c("0" = "0. Low", "1" = "1", "2" = "2",
                              "3" = "4. High"))

#### >>> Basic Model (with covariates and interactions for competence*win chance; Model 3B in Table 1) ####
# -> The full model is also displayed in Table B1 in the Supplementary Material (Model 3B)

Logit_2D <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp+ 
                  Competence*ExpCom_N + 
                  WinChaPref_Relative + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=df_PrevVote)
summary(Logit_2D)

#### >>> Figure 3.2 - marginal effects plot 3 ####
# Plot is based on Model "Logit_3B" above
intplot2D <- interplot(m = Logit_2D, var1 = "Competence", var2 = "ExpCom_N", hist = T,
                       adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived, expected electoral \n competition in constituency")+
  ylab("Marginal effect of party \n incompetence on insincere voting")+
  ggtitle("3.2- Party incompetence*Electoral \n competition")+
  scale_x_continuous(labels=c("0" = "0. Low", "1" = "1", "2" = "2",
                            "3" = "4. High"))



#### >>> Save Figures ####
# Use grid arrange to put interaction plots together:

## Figure 2 (interaction with win chances)
png("Figure2_Interactionplots.png",
    units="in", width=8, height=3, res=300)
grid.arrange(intplot2A, intplot2B, ncol=2)
dev.off()
#Save Figure 2 in Tiff Format
tiff("Figure2_Interactionplots_TIFF.tiff",
     units="in", width=8, height=3, res=300, compression = "lzw")
grid.arrange(intplot2A, intplot2B, ncol=2)
dev.off()


## Figure 3 (interaction with electoral competition)
png("Figure3_Interactionplots.png",
    units="in", width=8, height=3, res=800)
grid.arrange(intplot2C, intplot2D, ncol=2)
dev.off()
#Save Figure 3 in Tiff Format
tiff("Figure3_Interactionplots_TIFF.tiff",
     units="in", width=8, height=3, res=300, compression = "lzw")
grid.arrange(intplot2C, intplot2D, ncol=2)
dev.off()

#### >>> Generate Table 1 for the paper and Table B1 for Supplementary Material ####

# Note: The final tables (both in the main text and the supplementary material)
#       were manually formated.


#Prepare table for main Models 1, 1A, 2B, 3A, and 3B:
stargazer(Logit_2, Logit_2A, Logit_2B, Logit_2C, Logit_2D,   
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          notes = c("Table reports log odds from logistic regression models with standard errors in parentheses;
                    * p<0.05; ** p<0.01; *** p<0.001."),
          notes.append = F,
          model.numbers = F,
          column.labels = c("Model 1", "Model 2A", "Model 2B", "Model 3A", "Model 3B"),
          digits = 2,
          no.space = TRUE,
          single.row = T)


#------------------------------------------------------------------------------------------#
###########################################################################################/
#### /// 3C RBC1 - Party Preference                                                    #### 
###########################################################################################/
#------------------------------------------------------------------------------------------#

# The aim of this robustness check is to test if the theoreized mechanism of protest voting
# works regardless of which party respondents prefer most. I carry out two tests. First,
# I exclude respondents by their most preferred party, one at a time. Second, I add a categorical
# variable into the main model to control for which party respondents prefer most. All tests
# are repeated for the model testing hypothesis 1 in the paper.

#### Excluding respondents by their party preference:

# Without Conservatives:
Logit_RBC1_A <- glm(NonSince_FoUp_Dum ~ 
                      IdeoDis_FoUp + 
                      Competence + 
                      WinChaPref_Relative+ 
                      ExpCom_N + 
                      OutcomeImportance + 
                      OutcomeImportance_National + 
                      ID_HighestRatedParty_FoUp + 
                      LocalLeader_Congruence_FoUp + 
                      LeaderPartyPref_CongruenceCat_FoUp +
                      Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote[df_PrevVote$PartyPref_FoUp!=1,])
summary(Logit_RBC1_A)


# Without New Democratic Party:
Logit_RBC1_B <- glm(NonSince_FoUp_Dum ~ 
                      IdeoDis_FoUp + 
                      Competence + 
                      WinChaPref_Relative+ 
                      ExpCom_N + 
                      OutcomeImportance + 
                      OutcomeImportance_National + 
                      ID_HighestRatedParty_FoUp + 
                      LocalLeader_Congruence_FoUp + 
                      LeaderPartyPref_CongruenceCat_FoUp +
                      Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote[df_PrevVote$PartyPref_FoUp!=2,])
summary(Logit_RBC1_B)


# Without Liberals:
Logit_RBC1_C <- glm(NonSince_FoUp_Dum ~ 
                      IdeoDis_FoUp + 
                      Competence + 
                      WinChaPref_Relative+ 
                      ExpCom_N + 
                      OutcomeImportance + 
                      OutcomeImportance_National + 
                      ID_HighestRatedParty_FoUp + 
                      LocalLeader_Congruence_FoUp + 
                      LeaderPartyPref_CongruenceCat_FoUp +
                      Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote[df_PrevVote$PartyPref_FoUp!=3,])
summary(Logit_RBC1_C)

# Without Bloc Quebecois:
Logit_RBC1_D <- glm(NonSince_FoUp_Dum ~ 
                      IdeoDis_FoUp + 
                      Competence + 
                      WinChaPref_Relative+ 
                      ExpCom_N + 
                      OutcomeImportance + 
                      OutcomeImportance_National + 
                      ID_HighestRatedParty_FoUp + 
                      LocalLeader_Congruence_FoUp + 
                      LeaderPartyPref_CongruenceCat_FoUp +
                      Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote[df_PrevVote$PartyPref_FoUp!=4,])
summary(Logit_RBC1_D)


# Without Green Party:
Logit_RBC1_E <- glm(NonSince_FoUp_Dum ~ 
                      IdeoDis_FoUp + 
                      Competence + 
                      WinChaPref_Relative+ 
                      ExpCom_N + 
                      OutcomeImportance + 
                      OutcomeImportance_National + 
                      ID_HighestRatedParty_FoUp + 
                      LocalLeader_Congruence_FoUp + 
                      LeaderPartyPref_CongruenceCat_FoUp +
                      Gender + Age + Edu + Province,
                    family = "binomial",
                    data=df_PrevVote[df_PrevVote$PartyPref_FoUp!=5,])
summary(Logit_RBC1_E)


#### >>> Table C1 (Supplementary Material) #### 

#Prepare table for main Models RBC1A, RBC1B, RBC1C, RBC1D, and RBC1E:
stargazer(Logit_RBC1_A, Logit_RBC1_B, Logit_RBC1_C, Logit_RBC1_D, Logit_RBC1_E,   
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          notes = c("Table reports log odds from logistic regression models with standard errors in parentheses;
                    * p<0.05; ** p<0.01; *** p<0.001."),
          notes.append = F,
          model.numbers = F,
          column.labels = c("without Conservative",
                            "without NDP", 
                            "without Liberals",
                            "without BC",
                            "without Greens"),
          digits = 2,
          no.space = TRUE,
          single.row = T)


#### >>> Robustness Check 1 continued ####

#The following analysis includes a categorical variable ("PrefTypeFoUp") in main model
# (Results correspond to those reported in Table C2 in Supplementary Material):

# Regression Analysis - Robustness Check
Logit_RBC1_F <- glm(NonSince_FoUp_Dum ~ 
                    IdeoDis_FoUp + 
                    Competence + 
                    PrefTypeFoUp +
                    WinChaPref_Relative+ 
                    ExpCom_N + 
                    OutcomeImportance + 
                    OutcomeImportance_National + 
                    ID_HighestRatedParty_FoUp + 
                    LocalLeader_Congruence_FoUp + 
                    LeaderPartyPref_CongruenceCat_FoUp +
                    Gender + Age + Edu + Province,
                  family = "binomial",
                  data=df_PrevVote)
summary(Logit_RBC1_F)


#### >>> Table C1 (Supplementary Material ####
#Prepare table for main Model RBC1_F:
stargazer(Logit_RBC1_F,   
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          notes = c("Table reports log odds from logistic regression models with standard errors in parentheses;
                    * p<0.05; ** p<0.01; *** p<0.001."),
          notes.append = F,
          model.numbers = F,
          column.labels = c("RBC 2"),
          digits = 2,
          no.space = TRUE,
          single.row = T)


#------------------------------------------------------------------------------------------#
###########################################################################################/
#### /// 3D RBC3 - Re-Running the full Analyses on the Full Sample                         #### 
###########################################################################################/
#------------------------------------------------------------------------------------------#

# First, drop all reported abstainers from analyses (see paper for more information about
# why abstainers were excluded)
dfCan2015_RBC2 <- dfCan2015[dfCan2015$NonSince_FoUp!=2,]

# Before running the analyses, the variable party competence
# is renamned and its levels are renmaned, too, for plotting
# purposes:
names(dfCan2015_RBC2)[names(dfCan2015_RBC2) == "Comp_FoUp_Dummy"] <- "Competence"
levels(dfCan2015_RBC2$Competence) <- c("Most competent", ": Not most competent")

#### >>> Basic Model (Model D1A in Supplementary Material Section D) ####

Logit_RBC2_1 <- glm(NonSince_FoUp_Dum ~ IdeoDis_FoUp + Competence,
               family = "binomial",
               data=dfCan2015_RBC2)
summary(Logit_RBC2_1)


#### >>> Basic Model with covariates (Model D1B in Supplementary Material Section D) ####

Logit_RBC2_2 <- glm(NonSince_FoUp_Dum ~ 
                 IdeoDis_FoUp + 
                 Competence + 
                 WinChaPref_Relative+ 
                 ExpCom_N + 
                 OutcomeImportance + 
                 OutcomeImportance_National + 
                 ID_HighestRatedParty_FoUp + 
                 LocalLeader_Congruence_FoUp + 
                 LeaderPartyPref_CongruenceCat_FoUp +
                 Gender + Age + Edu + Province,
               family = "binomial",
               data=dfCan2015_RBC2)
summary(Logit_RBC2_2)

#### >>> Basic Model with covariates and interactions for expected win chances (Model D2A in Supplementary Material Section D) ####

Logit_RBC2_2A <- glm(NonSince_FoUp_Dum ~ 
                  IdeoDis_FoUp*WinChaPref_Relative + 
                  Competence + 
                  ExpCom_N + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=dfCan2015_RBC2)  
summary(Logit_RBC2_2A)


#### >>> Plot D1.1 (Supplementary Material) ####
intplot_RBC2_2A <- interplot(m = Logit_RBC2_2A, var1 = "IdeoDis_FoUp", var2 = "WinChaPref_Relative", 
                             hist = T, adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived chances of preferred \n party to win constituency")+
  ylab("Marginal effect of ideological \n incongruence on insincere voting")+
  ggtitle("D1.1- Ideological incongruence*Win \n chance")+
  scale_x_continuous(labels=c("0.00" = "0. None", "0.25" = "0.25", "0.50" = "0.50",
                              "0.75" = "0.75", "1.00" = "1. High"))

#### >>> Basic Model with covariates and interactions for competence*win chance (Model D2B in Supplementary Material Section D) ####

Logit_RBC2_2B <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp + 
                  Competence*WinChaPref_Relative + 
                  ExpCom_N + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=dfCan2015_RBC2)
summary(Logit_RBC2_2B)

#### >>> Plot D1.2 (Supplementary Material) ####
intplot_RBC2_2B <- interplot(m = Logit_RBC2_2B, var1 = "Competence", var2 = "WinChaPref_Relative", 
                             hist = T, adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived chances of preferred \n party to win constituency")+
  ylab("Marginal effect of party \n incompetence on insincere voting")+
  ggtitle("D1.2- Party incompetence*Win chance")+
  scale_x_continuous(labels=c("0.00" = "0. None", "0.25" = "0.25", "0.50" = "0.50",
                              "0.75" = "0.75", "1.00" = "1. High"))

#### >>> Basic Model with covariates and interactions for expected electoral competition (Model D3A in Supplementary Material Section D) ####

Logit_RBC2_2C <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp*ExpCom_N+ 
                  Competence + 
                  WinChaPref_Relative + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=dfCan2015_RBC2)  
summary(Logit_RBC2_2C)


#### >>> Plot D2.1 (Supplementary Material) ####
intplot_RBC2_2C <- interplot(m = Logit_RBC2_2C, var1 = "IdeoDis_FoUp", 
                             var2 = "ExpCom_N", hist = T, adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived, expected electoral \n competition in constituency")+
  ylab("Marginal effect of ideological \n incongruence on protest voting")+
  ggtitle("D2.1- Ideological incongruence* \n Electoral competition")+
  scale_x_continuous(labels=c("0" = "0. Low", "1" = "1", "2" = "2",
                              "3" = "4. High"))

#### >>> Basic Model with covariates and interactions for competence*win chance (Model D3B in Supplementary Material Section D) ####

Logit_RBC2_2D <- glm(NonSince_FoUp_Dum ~  
                  IdeoDis_FoUp+ 
                  Competence*ExpCom_N + 
                  WinChaPref_Relative + 
                  OutcomeImportance + 
                  OutcomeImportance_National + 
                  ID_HighestRatedParty_FoUp + 
                  LocalLeader_Congruence_FoUp + 
                  LeaderPartyPref_CongruenceCat_FoUp +
                  Gender + Age + Edu + Province,
                family = "binomial",
                data=dfCan2015_RBC2)
summary(Logit_RBC2_2D)

#### >>> Plot D2.2 (Supplementary Material) ####
intplot_RBC2_2D <- interplot(m = Logit_RBC2_2D, var1 = "Competence", 
                             var2 = "ExpCom_N", hist = T, adjCI = TRUE) + 
  theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed")+
  xlab("Perceived, expected electoral \n competition in constituency")+
  ylab("Marginal effect of party \n incompetence on insincere voting")+
  ggtitle("D2.2- Party incompetence*Electoral \n competition")+
  scale_x_continuous(labels=c("0" = "0. Low", "1" = "1", "2" = "2",
                              "3" = "4. High"))


#### >>> Save Figures for Supplementary Section D ####
# Use grid arrange to put interaction plots together for Supplementary Material D:

## Figure D.1 (interaction with win chances)
png("FigSuppD1_Interactionplots.png",
    units="in", width=8, height=3, res=800)
grid.arrange(intplot_RBC2_2A, intplot_RBC2_2B, ncol=2)
dev.off()
#Save Figure D.1 in Tiff Format
tiff("FigSuppD1_Interactionplots_TIFF.tiff",
     units="in", width=8, height=3, res=300, compression = "lzw")
grid.arrange(intplot_RBC2_2A, intplot_RBC2_2B, ncol=2)
dev.off()


## Figure D.2 (interaction with electoral competition)
png("FigSuppD2_Interactionplots.png",
    units="in", width=8, height=3, res=800)
grid.arrange(intplot_RBC2_2C, intplot_RBC2_2D, ncol=2)
dev.off()
#Save Figure D.2 in Tiff Format
tiff("FigSuppD2_Interactionplots_TIFF.tiff",
     units="in", width=8, height=3, res=300, compression = "lzw")
grid.arrange(intplot_RBC2_2C, intplot_RBC2_2D, ncol=2)
dev.off()

#### >>> Generate Table D1 for Supplementary Section D ####

#Prepare table for RBC2 Models D1A, D1B, D2A, D2B, D3A, and D3B:
stargazer(Logit_RBC2_1, Logit_RBC2_2, Logit_RBC2_2A, Logit_RBC2_2B, Logit_RBC2_2C, Logit_RBC2_2D,   
          star.cutoffs = c(0.05, 0.01, 0.001),
          star.char = c("*", "**", "***"),
          notes = c("Table reports log odds from logistic regression models with standard errors in parentheses;
                    * p<0.05; ** p<0.01; *** p<0.001."),
          notes.append = F,
          model.numbers = F,
          column.labels = c("Model C1A", "Model C1B", "Model C2A", "Model C2B", "Model C3A", "Model C3B"),
          digits = 2,
          no.space = TRUE,
          single.row = T)


#------------------------------------------------------------------------------------------#
###########################################################################################/
#### /// 3E RBC3 - Does preference strength matter?                                       #### 
###########################################################################################/
#------------------------------------------------------------------------------------------#

# In this section, I test whether the identified meachnism still holds after controlling
# for preference strenght (measured as the like/dislike score voters assigned to the 
# party they prefer most). The variable capturing this is called "PartyPref_Max" and its
# included as a control variable in the model used for the primary analysis. Prior to this,
# the code below generates a simple descriptive statistic to generate the average party rating
# respondents gave their most prefered parties, sorting by sincere and insincere votes:

#Descriptive statistic:

Summary.Data_Sec2H <- df_PrevVote %>%                       
  group_by(NonSince_FoUp_Dum) %>% 
  summarise(mean=mean(PartyPref_Max, na.rm=TRUE),
            median=median(PartyPref_Max, na.rm=TRUE),
            sd=sd(PartyPref_Max, na.rm=TRUE),
            min=min(PartyPref_Max, na.rm=TRUE),
            max=max(PartyPref_Max, na.rm=TRUE))
Summary.Data_Sec2H #lets look at the output

# These results are not reported in the paper or the supplementary material. They were, however,
# part of the response to one of the reviewers' questions on the relationship between strength
# of preferences and insincere voting. A broad reference to this was made in endnote 3 in the 
# paper. 



######################## END OF RSCRIPT #######################/